package com.card.game.log;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.card.game.BasePlayer;
import com.card.game.room.TggRoom;
import com.card.user.User;
import com.card.user.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年8月31日 下午4:39:32 
 * @version V1.0   
 */
public class TggResults {
	private Map<String,TggResult> resultMap = Maps.newConcurrentMap();
	
	private transient TggRoom room;
	
	private List<boolean[]> winIndex = Lists.newArrayList();
	
	public TggResults(TggRoom room) {
		this.room = room;
	}
	public void init() {
		if(!resultMap.isEmpty()) {
			return;
		}
		Vector<BasePlayer> pls = room.getPlayerList();
		for (int i = pls.size()-1; i >= 0; i--) {
			String userId = pls.get(i).getUserId();
			resultMap.put(userId, new TggResult(userId));
		}
	}
	
	public TggResult getTggResult(String userId) {
		TggResult temp = resultMap.get(userId);
		if(temp == null) {
			temp = new TggResult(userId);
			resultMap.put(userId, temp);
		}
		return temp;
	}
	
	public void addWinResult(boolean[] result) {
		this.winIndex.add(result);
	}
	public void clearZoushi() {
		this.winIndex.clear();
	}
	
	public List<TggResult> getTggResult() {
		return Lists.newArrayList(resultMap.values());
	}
	public List<boolean[]> getWinIndex() {
		return winIndex;
	}
	
	public List<TggResult> getSortRank() {
		List<TggResult> results = getTggResult();
		Collections.sort(results);
		return results;
	}
	
	public void saveScore() {
		List<TggResult> resultList = getTggResult();
		for (int i = resultList.size()-1; i >= 0; i--) {
			TggResult res = resultList.get(i);
			int score = res.score;
			if(score != 0) {
				User user = UserUtils.getUserByPrimaryKey(res.userId);
				if(user != null) {
					user.addScore(score);
				}
			}
		}
	}
}
